package com.speedricemanager.other.utils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.speedricemanager.R;
import com.speedricemanager.interfaces.DialogBackListener;
import com.speedricemanager.other.base.BaseRecyclerAdapter;
import com.speedricemanager.other.base.BaseRecyclerHolder;
import com.speedricemanager.other.base.RecycleViewDivider;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *  new DialogUtils(this).setData((String[]) null).setSelectClick(new DialogUtils.ClickListener() {
@Override
public void select(RxDialogSureCancel dialog, int position) {
Toast.makeText(LoginAty.this,"显示"+position,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setCommitClickListener("添加小号", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LoginAty.this,"哈哈哈哈或",Toast.LENGTH_SHORT).show();
}
}).show();
 */

public class DialogUtils {
    private RxDialogSureCancel rxDialogSureCancel;
    private PullLoadMoreRecyclerView lv;
    private Context context;
    private TextView dialog_commit,dialog_cancel;
    private DialogAdapter adapter;
    private ArrayList<String> arr;
    public DialogUtils(Context context){
        this.context=context;
          rxDialogSureCancel = new RxDialogSureCancel(context);
        View view=View.inflate(context, R.layout.dialog_item,null);
        rxDialogSureCancel.setContentView(view);
         lv =  view.findViewById(R.id.lv_dialog);
        arr=new ArrayList<>();
        init(context);
        dialog_commit=view.findViewById(R.id.dialog_commit);
        dialog_cancel=view.findViewById(R.id.dialog_cancel);
    }

    private void init(Context context) {
        lv.setLinearLayout();
        lv.setPullRefreshEnable(false);
        lv.setPushRefreshEnable(false);
        lv.getRecyclerView().addItemDecoration(new RecycleViewDivider(context, 1));
        adapter=new DialogAdapter(context, arr, R.layout.dialog_lv_item);
        lv.setAdapter(adapter);
    }

    public DialogUtils setData(String[] arr){
        if(arr!=null){
            adapter.clearAll(new ArrayList<String>(Arrays.asList(arr)));
        }
        return this;
    }
    public DialogUtils setData(ArrayList arr){
        if(TextUtils.isEmptyArr(arr)){
            adapter.clearAll(arr);
        }
        return this;
    }

    public DialogUtils setSelectClick(final ClickListener listener){
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                listener.select(rxDialogSureCancel,position);
            }
        });
        return this;
    }
    public DialogUtils setCommitClickListener(String commit,final ClickListener listener){
        dialog_commit.setText(commit);
        dialog_commit.setVisibility(View.VISIBLE);
        dialog_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.select(rxDialogSureCancel,0);
            }
        });
        return this;
    }

    public void show(){
        if(rxDialogSureCancel.isShowing()){
            rxDialogSureCancel.dismiss();
        }else{
            rxDialogSureCancel.show();
        }
    }


    //提示框 带有确定的提示框
    public static void showAlert(Context context,String conntent,final DialogBackListener listener) {
        new QMUIDialog.MessageDialogBuilder(context)
                .setTitle("提示")
                .setMessage(conntent)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        listener.fails();
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        listener.success("");
                        dialog.dismiss();
                    }
                })
                .show().setCancelable(false);
    }




public interface ClickListener{
    void select(RxDialogSureCancel dialog, int position);
}


    class DialogAdapter extends BaseRecyclerAdapter<String>{


        public DialogAdapter(Context context, List<String> list, int itemLayoutId) {
            super(context, list, itemLayoutId);
        }

        @Override
        public void convert(BaseRecyclerHolder holder, final String item, final int position, boolean isScrolling) {
            holder.setText(R.id.tv_lv,item);

        }
    }
}
